Problem
Intervals
Description
You are given closed, integer intervals and integers .
Write a program that:
reads the number of intervals, their end points and integers from the standard input, computes the minimal size of a set of integers which has at least common elements with interval , for each , writes the answer to the standard output.
Input
The first line of the input contains an integer () – the number of intervals. The following n lines describe the intervals. The line of the input contains three integers , and separated by single spaces and such that and .
Output
The output contains exactly one integer equal to the minimal size of set sharing at least elements with interval , for each .
Sample Input
1 | 5 |
Sample Output
1 | 6 |
标签:差分约束系统
Translation
给出个区间,试确定一个集合使得对于,第个区间内至少有个数,并使得此集合尽量小,输出最小大小。
Solution
首先用前缀和。表示从到中共选出多少个数到集合中。这样对于集合,有,于是我们可以从点到连一条权值为的边。因为题意是要满足所有的边,所以我们需要找最长路。
此题有一些细节问题。首先,找最长路需要起点和终点,我们需要找到这些集合覆盖的范围,即找到左端点(其实应该是左端点)的最小值和右端点的最大值,找到的最大值。此外,光有上述的那些边时无法构成一个连通图的,所以我们需要找一些隐含条件。可以发现有,,为了保持一致,应将后面的式子转化为大于等于,即,这样对于,从到连接一条权值为的路,从到连接一条权值为的路,之后就可以直接用找最长路了。
Code
1 |
|